package gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class calc {
    // private JFrame frame;
    public calc(){
    //    frame = new JFrame(null, null);
        JFrame f = new JFrame("计算器1");
        f.setVisible(true);
		f.setSize(400,400);
        f.setLocationRelativeTo(null);
        Container con = f.getContentPane();
        con.setLayout(new GridLayout(6,1));
        JPanel pan1 = new JPanel();
        JLabel title = new JLabel("简单计算器");
        title.setFont(new Font("宋体", Font.BOLD,30));
        pan1.add(title);
        con.add(pan1);


        //内容1
        JPanel pan2 = new JPanel();
        JLabel operate1 = new JLabel("操作数1");
        TextField number1 = new TextField(10);
		pan2.add(operate1);pan2.add(number1);
		con.add(pan2);

        
        //内容2
        JPanel pan3 = new JPanel();
        JLabel operate2 = new JLabel("操作数2");
        TextField number2 = new TextField(10);
		pan3.add(operate2);pan3.add(number2);
		con.add(pan3);



        //内容3
        JPanel pan4 = new JPanel();
        JButton button = new JButton("+");
        JButton button1 = new JButton("-");
        JButton button2 = new JButton("*");
        JButton button3 = new JButton("/");
        pan4.add(button);
        pan4.add(button1);
        pan4.add(button2);
        pan4.add(button3);
        con.add(pan4);


         //内容4
         JPanel pan5 = new JPanel();
         JLabel consequence = new JLabel("计算结果");
         TextField result  = new TextField(10);
         result.setEditable(false);
         pan5.add(consequence);pan5.add(result);
         con.add(pan5);


       


        //加法
        button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
                String text1 = number1.getText();
                String text2 = number2.getText();

                // 将文本内容转换为数字进行计算
                double number1 = Double.parseDouble(text1);
                double number2 = Double.parseDouble(text2);
                double result55 = number1 + number2; 
                String text = String.valueOf(result55);
                result.setText(text);
			}
            
        });


        
        //减法
        button1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
                String text3 = number1.getText();
                String text4 = number2.getText();

                // 将文本内容转换为数字进行计算
                double number1 = Double.parseDouble(text3);
                double number2 = Double.parseDouble(text4);
                double result5 = number1 - number2; 
                String text1 = String.valueOf(result5);
                result.setText(text1);
			}
            
        });



         //乘法
         button2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
                String text1 = number1.getText();
                String text2 = number2.getText();

                // 将文本内容转换为数字进行计算
                double number1 = Double.parseDouble(text1);
                double number2 = Double.parseDouble(text2);
                double result55 = number1 * number2; 
                String text = String.valueOf(result55);
                result.setText(text);
			}
            
        });

         //除法
         button3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
                String text1 = number1.getText();
                String text2 = number2.getText();

                // 将文本内容转换为数字进行计算
                double number1 = Double.parseDouble(text1);
                double number2 = Double.parseDouble(text2);
                double result55 = number1 / number2; 
                String text = String.valueOf(result55);
                result.setText(text);
			}
            
        });


         //最后
         JPanel pan6 = new JPanel();
         JButton button4 = new JButton("退出");
         pan6.add(button4);
         con.add(pan6);
        button4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                f.dispose();
            }
           
        });
    }
    






    
    

 
}